package com.chaos.system.service.convert;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.document.DefaultDocumentFormatRegistry;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.File;

@Slf4j
//@Service
@RequiredArgsConstructor
public class ConvertService {
    private final DocumentConverter documentConverter;

    @GetMapping("/exportReprotsPdf")
    public void exportPdf() throws Exception {
        String excelFilePath = "/Users/cuijian/Downloads/2.xlsx"; // Replace with the actual Excel file path
        String pdfFilePath = "/Users/cuijian/Downloads/output.pdf"; // Replace with the actual PDF file path


        try {

            // 定义输入和输出文件
            File inputFile = new File(excelFilePath);
            File outputFile = new File(pdfFilePath);

            if (!inputFile.exists()) {
                log.error("Input file does not exist: {}", inputFile.getAbsolutePath());
                return;
            }

            log.info("Starting conversion from {} to {}", inputFile.getAbsolutePath(), outputFile.getAbsolutePath());
            long startTime = System.currentTimeMillis();

            documentConverter.convert(inputFile).to(outputFile).as(DefaultDocumentFormatRegistry.PDF).execute();


            long endTime = System.currentTimeMillis();
            log.info("Conversion completed in {} ms", endTime - startTime);
        } catch (Exception e) {
            log.error("Error during conversion", e);
        }
    }

}
